define(['store'], function (store) {
    'use strict';
    function ctrl($scope, $stateParams, $ionicLoading, LocalCacheService, CommonService, WebService, $ionicHistory) {

        function init() {
            $scope.onBackKeyDown = function () {
                $ionicHistory.goBack();
                store.remove('account_code');
            };

            $scope.transAccount = {};

            $scope.account = {
                accountType: $stateParams.typeid,
                checked: false,
                accountCode: store.get('account_code') ? store.get('account_code') : ''
            };

            $scope.$watch("account.accountCode", function (newValue, oldValue) {
                if (newValue) {
                    store.set('account_code', newValue);
                }
            });

            $scope.user = LocalCacheService.getUser();
            var accountType = $scope.account.accountType;
            var exchange_kind = null;
            if (accountType == '1') {//沪A
                exchange_kind = '10';
            } else if (accountType == '2') {//深A
                exchange_kind = '20';
            }
            var params = {client_id: $scope.user.client_id, exchange_kind: exchange_kind};
            $ionicLoading.show();
            WebService.qryStockholderDetail(params).then(function (data) {
                $ionicLoading.hide();
                $scope.transAccount.status = data.open_status;
                if (data.open_status == '1') {//已开通
                    if (exchange_kind == '10') {
                        if (data.regflag == "1") {
                            $scope.transAccount.subtext = "您已有上海A股账户（" + data.stock_account + "）。如有疑问，请拨打客服热线：400-800-5000。";
                        } else if (data.regflag == "0") {
                            if (data.csdc_time_flag == "1") {// 查询是否是交易时间
                                if (data.csdc_relation_flag == "0") {
                                    $scope.transAccount.subtext = "您已有上海A股账户（" + data.stock_account + "），但未指定在天风证券，可能是由于您之前提交过新开户或转户申请，系统尚未处理完成。请您在下一交易日查询处理结果（处理成功时账号会显示在“我的账户”首页），如果未开户或转户成功，可以再次提交申请。如有疑问，请拨打客服热线：400-800-5000。";
                                } else if (data.csdc_relation_flag == "1") {
                                    $scope.transAccount.subtext = "您已有上海A股账户（" + data.stock_account + "）,但已指定在其它证券公司。如有疑问，请拨打客服热线：400-800-5000。";
                                }
                            } else if (data.csdc_time_flag == "0") {
                                $scope.transAccount.subtext = "您已有上海A股账户（" + data.stock_account + "），但未指定在天风证券，可能是由于您之前提交过新开户或转户申请，系统尚未处理完成。请您在下一交易日查询处理结果（处理成功时账号会显示在“我的账户”首页），如果未开户或转户成功，可以再次提交申请。如有疑问，请拨打客服热线：400-800-5000。";
                            }
                        }
                    } else if (exchange_kind == '20') {
                        $scope.transAccount.subtext = "您已有深圳A股账户（" + data.stock_account + "）。如有疑问，请拨打客服热线：400-800-5000。";
                    }
                } else if (data.open_status == '9') {//处理中
                    $scope.transAccount.subtext = "您已经提交了转户申请，系统正在处理中，请耐心等待处理结果。如有疑问，请拨打客服热线：400-800-5000";
                }
                /*else if(data.open_status=='2'){//处理失败
                 CommonService.showConfig({message:"很抱歉，您之前的转户申请处理失败！失败原因：" +data.op_content + "。如要继续转户，请重新提交转户申请。"});
                 }*/
            }, function () {
                $ionicLoading.hide();
                $scope.transAccount.status = '5';
                $scope.transAccount.subtext = "获取信息错误";
            });
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init()
        });

        $scope.submit = function () {
            //提交转户信息
            store.remove('account_code');
            var accountType = $scope.account.accountType;
            var exchange_kind = null;
            if (accountType == '1') {//沪A
                exchange_kind = '10';
            } else if (accountType == '2') {//深A
                exchange_kind = '20';
            }
            var params = {exchange_kind: exchange_kind, sc_open_flag: 2, stock_account: $scope.account.accountCode};
            $ionicLoading.show();
            WebService.addStockAccount(params).then(function (data) {
                $ionicLoading.hide();
                if (data.error_info) {
                    $scope.transAccount.status = 0;
                    $scope.transAccount.subtext = data.error_info;
                } else {
                    CommonService.showComplete('../view/template/transtemplate.html', '返回首页', 'tab.account-index');
                }
            }, function () {
                $ionicLoading.hide();
                CommonService.showAlert({message: "提交失败"});
            });
        }
    }

    ctrl.$inject = ['$scope', '$stateParams', '$ionicLoading', 'LocalCacheService', 'CommonService', 'WebService', '$ionicHistory'];
    return ctrl;
});